package com.holly.common_view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CompoundButton;

import java.util.ArrayList;
import java.util.List;

/**
 * 单选换行
 * 2016/12/10 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */

public class AutoWrapMultiChoiceLayout extends AutoWrapLayout {
    protected OnMultiCheckedChangedListener mOnMultiCheckedChangedListener;

    public AutoWrapMultiChoiceLayout(Context context) {
        super(context);
    }

    public AutoWrapMultiChoiceLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoWrapMultiChoiceLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void init() {
        super.init();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        //设置默认选中第一个
    }

    //缓存选中的View
    private List<View> mCheckedViewList = new ArrayList<>();
    //缓存未选中的View
    private List<View> mUnCheckedViewList = new ArrayList<>();


    @Override
    public void notifyDataChanged() {
        super.notifyDataChanged();
        mCheckedViewList.clear();
        mUnCheckedViewList.clear();
        //刷新完后设置Checked为第一个
        for (int i = 0; i < mChildViewList.size(); i++) {
            if (mChildViewList.get(i) instanceof CompoundButton) {
                CompoundButton button = (CompoundButton) mChildViewList.get(i);
                button.setOnCheckedChangeListener(null);
                if (button.isChecked()) {
                    button.setChecked(false);
                }
                mUnCheckedViewList.add(button);
                button.setOnCheckedChangeListener(new SimpleCheckedChangedListener(i));
            }
        }
    }



    public List<View> getCheckedViewList() {
        return mCheckedViewList;
    }

    public List<View> getUnCheckedViewList() {
        return mUnCheckedViewList;
    }

    public void setCheckedPosition(int position, boolean isChecked) {
        if (position < mChildViewList.size()) {
            if (mChildViewList.get(position) instanceof CompoundButton) {
                CompoundButton button = (CompoundButton) mChildViewList.get(position);
                if (button.isChecked() != isChecked) {
                    button.setChecked(isChecked);
                }
            } else {
                throw new IllegalArgumentException("setCheckedPosition: the view of position must be CompoundButton or its child");
            }
        } else {
            throw new IndexOutOfBoundsException("setCheckedPosition index = " + position + " but child count = " + mChildViewList.size());
        }
    }

    public void setCheckedAll(boolean isChecked){
        if (isChecked) {
            int size = mUnCheckedViewList.size();
            for (int i = 0; i < size; i++) {
                CompoundButton button = (CompoundButton) mUnCheckedViewList.get(0);
                button.setChecked(isChecked);
            }
        } else {
            int size = mCheckedViewList.size();
            for (int i = 0; i < size; i++) {
                CompoundButton button = (CompoundButton) mCheckedViewList.get(0);
                button.setChecked(isChecked);
            }
        }
    }

    public void setOnMultiCheckedChangedListener(OnMultiCheckedChangedListener onMultiCheckedChangedListener) {
        mOnMultiCheckedChangedListener = onMultiCheckedChangedListener;
    }

    //单选逻辑在这个监听中
    class SimpleCheckedChangedListener implements CompoundButton.OnCheckedChangeListener {
        private int mPosition;

        public SimpleCheckedChangedListener(int position) {
            this.mPosition = position;
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                if (!mCheckedViewList.contains(buttonView)) {
                    mCheckedViewList.add(buttonView);
                    mUnCheckedViewList.remove(buttonView);
                }
            } else {
                if (!mUnCheckedViewList.contains(buttonView)) {
                    mUnCheckedViewList.add(buttonView);
                    mCheckedViewList.remove(buttonView);
                }
            }
            if (mOnMultiCheckedChangedListener != null) {
                mOnMultiCheckedChangedListener.onCheckedChanged(buttonView, isChecked, mPosition);
            }
        }
    }

    public interface OnMultiCheckedChangedListener {
        //选中的位置
        void onCheckedChanged(CompoundButton buttonView, boolean isChecked, int position);
    }
}
